const { conn } = require('../gather/mongo');

// 获取收藏标题列表
exports.getColloctList = (req, res) => {
    const sql = `SELECT * FROM  collect_title_list WHERE user_id = ${req.query.UserId}`;
    conn.query(sql, (err, result) => {
        if (err) {
        } else {
            res.send({
                code: 200,
                data: result
            })
        }
    })
}
// 根据收藏标题列表查找收藏内容
exports.getCollectContent = (req, res) => {
    const { titleId, UserId } = req.body;
    const sql = `SELECT * FROM  collect_list WHERE parent_id = ${titleId} AND user_id = ${UserId}`;
    conn.query(sql, (err, result) => {
        if (err) {
        } else { }
        res.send({
            code: 200,
            data: result
        })
    })
}

// 新增收藏网站
exports.addCollect = (req, res) => {
    const { webName, webUrl, titleId, UserId } = req.body;
    console.log();
    const sql = `INSERT INTO collect_list (collect_name,collect_url, user_id,parent_id) VALUES ('${webName}','${webUrl}', ${UserId},${titleId})`;
    conn.query(sql, (err, result) => {
        if (err) {
            res.send({
                status: 0,
                message: '添加收藏失败'
            })
        } else {
            res.send({
                code: 200,
                message: '添加收藏成功'
            })
        }
    })
}
// 新增收藏网站标题
exports.addCollectTitle = (req, res) => {
    const { titleName, UserId } = req.body;
    console.log(titleName);
    const sql = `INSERT INTO collect_title_list (collect_title, user_id) VALUES ('${titleName}',${UserId})`;
    conn.query(sql, (err, result) => {
        if (err) {
            res.send({
                status: 0,
                message: '添加收藏失败'
            })
        } else {
            console.log(111);
            res.send({
                code: 200,
                message: '添加收藏成功'
            })
        }
    })
}
// 删除收藏网站标题
exports.RemoveCollectTitle = (req, res) => {
    const { titleId, UserId } = req.body;
    // const sql = `INSERT INTO collect_title_list (collect_title, user_id) VALUES ('${titleName}',${UserId})`;
    const sql = `DELETE FROM collect_title_list WHERE id = ${titleId} AND user_id = ${UserId}`;
    conn.query(sql, (err, result) => {
        if (err) {
            res.send({
                status: 0,
                message: '删除网站标题失败'
            })
        } else {
            res.send({
                code: 200,
                message: '删除网站标题成功'
            })
        }
    })
}
// 删除收藏网站标题
exports.RemoveCollectConent = (req, res) => {
    const { ContentId, UserId, titleId } = req.body;
    // const sql = `INSERT INTO collect_title_list (collect_title, user_id) VALUES ('${titleName}',${UserId})`;
    const sql = `DELETE FROM collect_list WHERE collect_id = ${ContentId} AND user_id = ${UserId} AND parent_id = ${titleId}`;
    conn.query(sql, (err, result) => {
        if (err) {
            res.send({
                status: 0,
                message: '删除网站内容失败'
            })
        } else {
            res.send({
                code: 200,
                message: '删除网站内容成功'
            })
        }
    })
}
// 修改收藏网站信息
exports.UpdateCollectContent = (req, res) => {
    const { webName, webUrl, titleId, UserId, ContentId } = req.body;
    const sql = `UPDATE collect_list SET collect_name = '${webName}',collect_url = '${webUrl}' WHERE collect_id = ${ContentId} AND user_id = ${UserId} AND parent_id = ${titleId}`;
    conn.query(sql, (err, result) => {
        if (err) {
            res.send({
                status: 0,
                message: '修改网站内容失败'
            })
        } else {
            res.send({
                code: 200,
                message: '修改网站内容成功'
            })
        }
    })
}